doseresNMA antidep/drnma.plot.sucra.R

# Sucra plot: the absolute response curve of all drugs 
sucraPlot <- function(x=x,ref.lab='placebo',refclass.lab='placebo',drug.lab=drug.lab, class.lab=NULL,class.effect=F, ...){
  
  #-- Prepare data to plot
  # reference numeric index
  ref.index <- which(levels(as.factor(drug.lab))==ref.lab)
  
  # 1. y-axis: p.drug 
  plotdata <- m$BUGSoutput$summary %>% 
    t() %>% 
    data.frame() %>% 
    select(starts_with('p.drug'))%>%
    t()%>% 
    data.frame() 
  
  # 2. x-axis: doses per drug
  ndrugs <- length(drug.lab)-1 # number of drugs
  max.dose <- unlist(data%>%
                       group_by(drug)%>%
                       group_map(~round(max(.x$dose,na.rm = T))))[-ref.index]
  plotdata$dose <- unlist(
    sapply(1:ndrugs, 
           function(i) seq(0,max.dose[i],l=nd),
           simplify = FALSE
    )
  )
  # 3. labels: drug name
  plotdata$drug <- rep(drug.lab[drug.lab!=ref.lab],each=nd)
  
  
  #-- plot     
 ggplot(data=plotdata, aes(x=dose, y=mean, group=drug)) +
    geom_line(aes(color=drug)) +
    theme_bw()+
    labs(y="Predicted absolute response", x="Dose")
  
}
htx-r/doseresNMA documentation built on Jan. 28, 2021, 5:32 a.m.